package dateStructure;

import java.util.Scanner;

public class sequence {
    public static int Maxsize = 107;

    private static class Ele {
        int elem[];
        int length;
    }
    private static Ele initList(Ele s) {
        s = new Ele();
        s.elem = new int[Maxsize];
        if (s.elem == null)
            return s;
        s.length = 0;
        return s;
    }

    private static boolean createList(Ele s) {
        int i = 0;
        int x=-1;
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数字：");
        while (scanner.hasNext()) {
            x=scanner.nextInt();
            if (x== -1)
                return true;
            if(s.length==Maxsize)
                break;
            s.elem[i++] =x;
            s.length++;
        }
        return true;
    }

    private static boolean listInsert(Ele s, int i, int e) {
        if (i < 0 || i > s.length + 1)///i表示的是下标+1 可以插在末尾
            return false;
        if (s.length == Maxsize)
            return false;
        for (int j = s.length-1 ; j >=i-1; j--)
            s.elem[j+1] = s.elem[j];
        s.elem[i - 1] = e;
        s.length++;
        return true;
    }

    private static boolean listDelete(Ele s, int i) {
        if (i < 0 || i > s.length+1)
            return false;
        for (int j = i-1; j < s.length; j++)
            s.elem[j] = s.elem[j+1];
        s.length--;
        return true;
    }
    public static int locateElem(Ele s,int e){
        for(int i=0;i<s.length;i++){
            if(s.elem[i]==e)
                return i+1;
        }
        return -1;
    }
    public static int getElem(Ele s, int i) {
        if (i < 0 || i > s.length)
            return -1;
        return s.elem[i - 1];
    }
    public static void print(Ele s){
        for(int i=0;i<s.length;i++)
            System.out.print(s.elem[i]+" ");
        System.out.println();
    }
    public static void main(String[] args) {
        Ele myl = null;
        int choose=-1,x,i;
        System.out.print("1:初始化\n2:创建\n3:取值\n4:删除\n5:查找\n6:插入\n7:输出\n");
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            choose=scanner.nextInt();
            if(choose==-1)
                break;
            switch (choose)
            {
                case 1:
                    if((myl=initList(myl))!=null){
                        System.out.println("初始化成功！");
                    }
                    else
                        System.out.println("初始化失败！");
                    break;
                case 2:
                    if(createList(myl)){
                        System.out.println("创建完成！");
                    }
                    else{
                        System.out.println("创建失败！");
                    }
                    break;
                case 3:
                    x=scanner.nextInt();
                    if(getElem(myl,x)!=-1)
                        System.out.println("所取的值为"+getElem(myl,x));
                    else
                        System.out.println("没有这个数！");
                    break;
                case 4:
                    System.out.println("请输入要删除元素的位置：");
                    i=scanner.nextInt();
                    if(listDelete(myl,i)){
                        System.out.println("成功删除！");
                    }
                    else{
                        System.out.println("删除失败！");
                    }
                    break;
                case 5:
                    System.out.println("请输入要查找的数字");
                    i=scanner.nextInt();
                    if(locateElem(myl,i)!=-1){
                        System.out.println("查找的数位置为"+locateElem(myl,i));
                    }
                    else{
                        System.out.println("查找失败！");
                    }
                    break;
                case 6:
                    System.out.print("请输入要插入的位置和数字：");
                    i=scanner.nextInt();
                    x=scanner.nextInt();
                    if(listInsert(myl,i,x)){
                        System.out.println("插入成功！");
                    }
                    else{
                        System.out.println("插入失败！");
                    }
                    break;
                case 7:
                    print(myl);
            }
        }
    }
}

